package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;

/**
 * Created by jack on 2018/3/12
 */

public class CashFunctionAdapter extends RecyclerView.Adapter<CashFunctionAdapter.CashFunctionViewHolder> {

    private List<HomeBean.BlockListBean> mDataList;

    private int mIndex;   //页数下标

    private int mPageSize = 4;  //每页显示最大条目个数

    private Context mContext;

    private OnCashFunctionAdapterClickListener onCashFunctionAdapterClickListener;

    public CashFunctionAdapter(Context context, List<HomeBean.BlockListBean> dataList, int index) {
        this.mContext = context;
        this.mDataList = dataList;
        this.mIndex = index;
    }

    @Override
    public CashFunctionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CashFunctionViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_cash_function, parent, false));
    }

    @Override
    public void onBindViewHolder(final CashFunctionViewHolder holder, int position) {
        final int pos = position + mIndex * mPageSize;
        HomeBean.BlockListBean blockListBean = mDataList.get(pos);
        holder.tvContent.setText(FRString.valueOf(blockListBean.getConfig_cn()));
        if (FRString.isEmpty(blockListBean.getLogo())) {
            return;
        }
        String corner = blockListBean.getCorner();
        if (TextUtils.isEmpty(corner)) {
            holder.tvTag.setVisibility(View.GONE);
        } else {
            holder.tvTag.setVisibility(View.VISIBLE);
            holder.tvTag.setText(corner);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=onCashFunctionAdapterClickListener){
                    onCashFunctionAdapterClickListener.onClick(v,pos);
                }
            }
        });

        if (blockListBean.getLogo().contains(".gif")) {
            FRImageLoad.loadImageGif(FRString.valueOf(blockListBean.getLogo()), holder.ivAvatar);
        } else {
            FRImageLoad.loadImage(FRString.valueOf(blockListBean.getLogo()), holder.ivAvatar);
        }
    }

    public void setOnCashFunctionAdapterClickListener(OnCashFunctionAdapterClickListener onCashFunctionAdapterClickListener) {
        this.onCashFunctionAdapterClickListener = onCashFunctionAdapterClickListener;
    }

    public interface OnCashFunctionAdapterClickListener {
        void onClick(View view,int position);
    }

    @Override
    public int getItemCount() {
        return mDataList.size() > (mIndex + 1) * mPageSize ? mPageSize : (mDataList.size() - mIndex * mPageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + mIndex * mPageSize;
    }

    public class CashFunctionViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivAvatar;
        private TextView tvContent;
        private TextView tvTag;

        public CashFunctionViewHolder(View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.icf_iv_avatar);
            tvContent = itemView.findViewById(R.id.icf_tv_content);
            tvTag = itemView.findViewById(R.id.icf_tv_tage);
        }
    }
}
